package com.culture.config.wrapper;

import java.util.Optional;

import com.culture.config.common.util.BeanUtil;
import com.culture.config.dao.entity.App;
import com.culture.config.dao.entity.Property;
import com.culture.config.model.po.*;
import com.culture.config.util.AuthenticationUtil;

import org.apache.commons.lang3.StringUtils;

import lombok.experimental.UtilityClass;

@UtilityClass
public class PropertyWrapper {
	public static Property wrapPropertyQuery(App app, String profile, String label) {
		return new Property(app.getName(),profile,label).setAppId(app.getId());
	}
	
	public static Property wrapPropertyQuery(PropertyPageParam param) {
		if(StringUtils.isBlank(param.getProfile())){
			param.setProfile(null);
		}
		if(StringUtils.isBlank(param.getLabel())){
			param.setLabel(null);
		}
		if(StringUtils.isBlank(param.getPropertyKey())){
			param.setPropertyKey(null);
		}
		Property property = new Property(param.getAppId(), param.getProfile(), param.getLabel());
		property.setPropertyKey(param.getPropertyKey());
		return property;
	}

	public static Property wrapPropertyQuery(PropertiesParam param) {		
		return new Property(param.getAppId(), param.getProfile(), param.getLabel());
	}	
	
	public static Property wrapPropertyUpdate(PropertyUpdateParam param) {
		Property property = BeanUtil.copyProperties(param, Property.class);
		Optional.ofNullable(param.getStatus()).ifPresent(property::setDeleted);
		Optional.ofNullable(param.getValueType()).ifPresent(property::setValueType);
		AuthenticationUtil.setUpdateFieldInfo(property);		
		return property;
	}

	public static Property wrapPropertyUpdate(Property oldProperty,String value) {
		Property property = new Property(oldProperty.getId());	
		property.setPropertyValue(value);
		AuthenticationUtil.setUpdateFieldInfo(property);		
		return property;
	}
	
	public static Property wrapPropertyAdd(PropertyAddParam param) {
		Property property = BeanUtil.copyProperties(param, Property.class);
		property.setDeleted(0);
		AuthenticationUtil.setInsertFieldInfo(property);		
		return property;
	}

	public static Property wrapPropertyAdd(App app, Property newProperty,PropertiesUpdateParam param) {
		Property property = BeanUtil.copyProperties(newProperty, Property.class);
		property.setAppId(app.getId());
		property.setApplication(app.getName());
		property.setLabel(param.getLabel());
		property.setProfile(param.getProfile());
		property.setDeleted(0);
		AuthenticationUtil.setInsertFieldInfo(property);		
		return property;
	}

	public static Property wrapPropertyCopy(Property param,PropertySyncPO syncPO) {
		Property property = BeanUtil.copyProperties(param, Property.class);	
		property.setProfile(syncPO.getTargetProfile());
		property.setLabel(syncPO.getTargetLabel());
		AuthenticationUtil.setInsertFieldInfo(property);		
		return property;
	}

	public static Property wrapPropertyCopyUpdate(Property param, Property source) {
		Property property = BeanUtil.copyProperties(param, Property.class);	
		property.setId(source.getId());
		AuthenticationUtil.setUpdateFieldInfo(property);		
		return property;
	}
}
